/**
 * 传播力-报道统计
 */
;
(function($, window, document, undefined) {
    var comChart;

    var TVRetainedStatistic = function(el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons',
            chartOption: {


                calculable: true,
                tooltip: {
                    trigger: 'axis'
                },
                dataZoom: {
                    show: true,
                    realtime: true,
                    start: 0,
                    end: 100
                },
                legend: {
                    data: ['累计留存', '同比留存']
                },
                xAxis: [{
                    type: 'category',
                    data: []
                }],
                yAxis: [{
                    type: 'value'
                }],
                series: [{
                    name: '同比留存',
                    type: 'line',
                    smooth: true,
                    itemStyle: {
                        normal: {
                            areaStyle: {
                                type: 'default'
                            }
                        }
                    },
                    data: []
                }, {
                    name: '累计留存',
                    type: 'line',
                    smooth: true,
                    itemStyle: {
                        normal: {
                            areaStyle: {
                                type: 'default'
                            }
                        }
                    },
                    data: []
                }]

            }
        };
        this.options = $.extend({}, this.defaults, opts);
    };

    TVRetainedStatistic.prototype = {
        init: function() {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == 'true') {
                return me;
            }

            me.initChart();
            me.bindEvents();


            el.attr('data-initialized', 'true');
            return me;
        },


        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function() {
            var me = this,
                el = me.el;

        },


        /**
         * 格式化数据
         * @param  {[type]} data [description]
         * @return {[type]}      [description]
         */
        formatData: function(originData) {
            var me = this,
                el = me.el,
                chartOption = me.options.chartOption,
                keys = ['accumulated_retained', 'compared_retained'],
                i, i, len1, len2;

            // 初始化series
            $.each(keys, function(i, key) {
                chartOption.series[i].data = [];
            });
            for (i = 0, len1 = originData.length; i < len1; i++) {
                for (j = 0, len2 = keys.length; j < len2; j++) {
                    chartOption.series[j].data.push(originData[i][keys[j]]);
                }
            }

            // 初始化xaxis
            for (i = 0, len1 = originData.length; i < len1; i++) {
                chartOption.xAxis[0].data.push(originData[i]['date']);
            }

            return chartOption;
        },

        /**
         * [initChart description]
         * @return {[type]} [description]
         */
        initChart: function() {
            var me = this,
                el = me.el,
                option = {};

            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);
        }
    };

    $.fn.TVRetainedStatistic = function(opts) {
        var me = this,
            el = me.el,
            comTVRetainedStatistic;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comTVRetainedStatistic = new TVRetainedStatistic(this, opts);
        }

        return comTVRetainedStatistic.init();
    }
})(jQuery, window, document);
